package com.yanqu.road.entity.grandchild;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class GrandChildChampionRankListModel {

    private final Object lock = new Object();

    private int groupId;
    private LinkedList<GrandChildChampion> rankList = new LinkedList<>();
    private int maxRank;
    private Comparator sort;

    public GrandChildChampionRankListModel(int groupId, int maxRank, Comparator sort) {
        this.groupId = groupId;
        this.maxRank = maxRank;
        this.sort = sort;
    }

    public void rankChange(GrandChildChampion userData) {
        boolean change = false;
        synchronized (lock) {
            // 已有数据
            for (GrandChildChampion tmpData : rankList) {
                if (tmpData.getUserId() == userData.getUserId()) {
                    tmpData.setCount(userData.getCount());
                    tmpData.setUpdateTime(userData.getUpdateTime());
                    change = true;
                    break;
                }
            }
            if (!change) {
                // 没满
                if (rankList.size() < maxRank) {
                    GrandChildChampion tmpData = new GrandChildChampion();
                    tmpData.setServerId(userData.getServerId());
                    tmpData.setUserId(userData.getUserId());
                    tmpData.setCount(userData.getCount());
                    tmpData.setUpdateTime(userData.getUpdateTime());
                    rankList.add(tmpData);
                    change = true;
                } else {
                    // 比最后一名强
                    GrandChildChampion tmpData = rankList.getLast();
                    if (userData.getCount() > tmpData.getCount()
                            || userData.getCount() == tmpData.getCount() && userData.getUpdateTime() < tmpData.getUpdateTime()) {
                        tmpData.setServerId(userData.getServerId());
                        tmpData.setUserId(userData.getUserId());
                        tmpData.setCount(userData.getCount());
                        tmpData.setUpdateTime(userData.getUpdateTime());
                        change = true;
                    }
                }
            }

            if (change) {
                rankList.sort(sort);
            }
        }
    }

    public void setRankList(List<GrandChildChampion> list) {
        synchronized (lock) {
            rankList = new LinkedList<>(list);
            rankList.sort(sort);
            if (rankList.size() > maxRank) {
                rankList.removeLast();
            }
        }
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public LinkedList<GrandChildChampion> getRankList() {
        return new LinkedList<>(rankList);
    }

    public void setRankList(LinkedList<GrandChildChampion> rankList) {
        this.rankList = rankList;
    }

    public int getMaxRank() {
        return maxRank;
    }

    public void setMaxRank(int maxRank) {
        this.maxRank = maxRank;
    }
}
